//
//  XjSegment.m
//  QinghaiTourism
//
//  Created by xujingjing on 16/4/4.
//  Copyright © 2016年 xujingjing. All rights reserved.
//

#import "XjSegment.h"
#import "UIView+FindNav.h"
#define BtnTag 1001

@interface XjSegment()<UIPageViewControllerDataSource,UIPageViewControllerDelegate,UIScrollViewDelegate>{

    UIButton *btnTitle;//标题


    
}
@property (nonatomic, strong)NSArray *btnArr;

@property(nonatomic,strong) NSArray *viewControllerArray;

@property (nonatomic, strong)UIPageViewController *pageController;
@property (nonatomic, assign)NSInteger currentPageIndex;



@end


@implementation XjSegment
-(instancetype)initWithControllersArr:(NSArray *)controllerArrs btnArr:(NSArray *)btnArr{
    self = [super init];
    if (self) {
        

     //   self.frame = self.navigationController.view.frame;
        self.backgroundColor = [UIColor yellowColor];
            self.viewControllerArray = controllerArrs;
            self.btnArr = btnArr;
       
        
            [self initMainController];
            [self setupPageViewController];
      

        
    }else{
    
    
    }

    return self;
}


- (UIPageViewController *)pageController{
    if (!_pageController) {
        _pageController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
        _pageController.view.frame = CGRectMake(0, 40, SCREEN_WIDTH, SCREENH_HEIGHT-40);//-5/-5
        _pageController.delegate = self;
        _pageController.dataSource = self;
        
                        _pageController.view.layer.borderWidth = 3;
                        _pageController.view.layer.borderColor = [UIColor redColor].CGColor;
        
        [_pageController setViewControllers:@[[self.viewControllerArray objectAtIndex:_currentPageIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
        
    }
    return _pageController;
}
//初始化导航控制器
-(void)initMainController{
    
    UILabel *theLine = [[UILabel alloc]init];
    for (int i = 0; i < self.btnArr.count; i ++) {
        btnTitle = [UIButton buttonWithType:UIButtonTypeCustom];
        btnTitle.backgroundColor = [UIColor whiteColor];
        [btnTitle setTitle:self.btnArr[i] forState:UIControlStateNormal];
        [btnTitle setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btnTitle setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
        btnTitle.titleLabel.font = [UIFont boldSystemFontOfSize:13];
        btnTitle.frame = CGRectMake(SCREEN_WIDTH/_btnArr.count*i,0,SCREEN_WIDTH/_btnArr.count,38);
        btnTitle.tag = BtnTag + i;
        if (i == _currentPageIndex) {
            btnTitle.selected = YES;
            theLine .frame = CGRectMake(btnTitle.frame.origin.x, 40-2, btnTitle.frame.size.width, 2);
            theLine.tag = 2000;
        }
        [btnTitle addTarget:self action:@selector(changeControllerClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnTitle];
    }
    theLine.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:theLine];
}
-(NSArray *)viewControllerArray{
    if (!_viewControllerArray) {
        _viewControllerArray = self.viewControllerArray;
    }
    return _viewControllerArray;

}
-(void)setupPageViewController{
   // [self.navigationController addChildViewController:self.pageController];
    [self addSubview:self.pageController.view];
    [self syncScrollView];
}
-(void)syncScrollView{
    for (UIView *view in self.pageController.view.subviews) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            UIScrollView *pageScrollView = (UIScrollView *)view;
            pageScrollView.delegate = self;
            pageScrollView.scrollsToTop=NO;
        }
    }
}
-(void)changeControllerClick:(id)sender{
    UIButton *btn = (UIButton *)sender;
    NSInteger tempIndex = _currentPageIndex;
    __weak typeof (self) weakSelf = self;
    NSInteger nowTemp = btn.tag - BtnTag;
    if (nowTemp > tempIndex) {
        [_pageController setViewControllers:@[[self.viewControllerArray objectAtIndex:nowTemp]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
            if (finished) {
                [weakSelf updateCurrentPageIndex:nowTemp];
            }
        }];
        
    }else if (nowTemp < tempIndex){
        [_pageController setViewControllers:@[[self.viewControllerArray objectAtIndex:nowTemp]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:^(BOOL finished) {
            if (finished) {
                [weakSelf updateCurrentPageIndex:nowTemp];
            }
        }];
    }
}

-(void)updateCurrentPageIndex:(NSInteger)newIndex
{
    _currentPageIndex = newIndex;
    
    
    UIButton *btn = (UIButton *)[self viewWithTag:BtnTag+_currentPageIndex];
    for (int i = 0 ; i < self.btnArr.count; i ++) {
        UIButton *otherBtn = (UIButton *)[self viewWithTag:BtnTag + i];
        if (btn.tag == otherBtn.tag) {
            otherBtn.selected = YES;
        }else{
            otherBtn.selected = NO;
        }
    }
}
#pragma mark --------Scroll协议-------
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    NSInteger X = _currentPageIndex;
    UIButton *btn = (UIButton *)[self viewWithTag:X+BtnTag];
    [UIView animateWithDuration:0.2 animations:^{
        UIView *line = (UIView *)[self viewWithTag:2000];
        CGRect sizeRect = line.frame;
        sizeRect.origin.x = btn.frame.origin.x;
        line.frame = CGRectMake(btn.frame.origin.x, 40-2, btn.frame.size.width, 2);
    }];
}

#pragma mark - Page View Controller Data Source

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSInteger index = [self indexOfController:viewController];
    
    if ((index == NSNotFound) || (index == 0)) {
        return nil;
    }
    index--;
    return [_viewControllerArray objectAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSInteger index = [self indexOfController:viewController];
    index++;
    
    if (index == [_viewControllerArray count]) {
        return nil;
    }
    return [_viewControllerArray objectAtIndex:index];
}

-(void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    if (completed) {
        _currentPageIndex = [self indexOfController:[pageViewController.viewControllers lastObject]];
        [self updateCurrentPageIndex:_currentPageIndex];
        NSLog(@"当前界面是界面=== %ld",(long)_currentPageIndex);
    }
}

-(NSInteger)indexOfController:(UIViewController *)viewController
{
    for (int i = 0; i<[_viewControllerArray count]; i++) {
        if (viewController == [_viewControllerArray objectAtIndex:i])
        {
            return i;
        }
    }
    return NSNotFound;
}

- (NSArray *)btnArr{
    if (!_btnArr) {
        _btnArr =  self.btnArr;
    }
    return _btnArr;
}



@end
